﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebHoiChoMuaBan.Models;
using WebHoiChoMuaBan.Controllers.Global;
using WebHoiChoMuaBan.Controllers.Helpers;
using WebHoiChoMuaBan.Controllers.Utility;
using System.IO;

namespace WebHoiChoMuaBan.Controllers.Admin
{
    public class AdminManagementController : AdminController
    {
        //
        // GET: /AdminManagement/

        public ActionResult Index()
        {
            return View("~/Views/Admin/AdminManagement.aspx");
        }

        public ActionResult Create()
        {
            MemberTypeHelper memTypeHelper = new MemberTypeHelper();
            ViewData["MemType"] = new SelectList(memTypeHelper.GetAllMemberType(), "MemTypeId", "MemTypeName");
            return View("~/Views/Admin/AddAdmin.aspx");
        }

        [HttpPost]
        public ActionResult AddAdmin(FormCollection data, HttpPostedFileBase photo)
        {
            // TODO: Add insert logic here
            string pass = data.Get("password");
            string conPass = data.Get("conPassword");
            if (string.IsNullOrEmpty(pass) || pass.Length < 6)
                ModelState.AddModelError("password", "Hãy nhập Mật khẩu tối thiếu 6 ký tự.");
            if (!pass.Equals(conPass))
                ModelState.AddModelError("conPassword", "Hãy nhập mật khẩu 2 lần giống nhau");

            Member mem = new Member();
            mem.MemUsername = data.Get("username");
            mem.MemPasswordString = data.Get("password");
            mem.MemEmail = data.Get("email");
            mem.MemFullname = data.Get("fullname");
            mem.MemCellPhone = data.Get("cellPhone");
            mem.MemJoinDate = DateTime.Now;
            mem.MemTypeId = int.Parse(data.Get("ddlMemberType"));
            if (photo == null)
                mem.MemAvatar = Config.DEFAULT_AVATAR;
            else
            {
                ImageHelper imgHelper = new ImageHelper();
                if (imgHelper.CheckInfoIcon(photo))
                {
                    string path = Path.Combine(Server.MapPath("~/Content/Images/Upload/Thumbnail"), photo.FileName);
                    imgHelper.uploadImage(path, photo, 200, 100);
                    mem.MemAvatar = photo.FileName;
                }
            }

            MemberHelper memberHelper = new MemberHelper();
            memberHelper.Add(mem);
            memberHelper.Save();

            //string[] strUserType;
            //strUserType = data.Get("UserType").Split(',');
            //GroupHelper group = new GroupHelper();
            //Member_GroupHelper member_groupHelper = new Member_GroupHelper();
            //for (int i = 0; i < strUserType.Length; i++)
            //{
            //    if (strUserType[i].Equals("on"))
            //    {
            //        Member_Group member_group = new Member_Group();
            //        member_group.GroupID = group.GetGroupID(i + 1).GroupID;
            //        member_group.MemID = mem.MemId;
            //        member_groupHelper.Add(member_group);
            //        member_groupHelper.Save();
            //    }
            //}

            //Trở về lại trang danh sách Admin
            return Index();
        }

        public List<Group> getAllGroup()
        {
            return new AdminHelper().getAllGroup();
        }


        public ActionResult RightManager(int grpId)
        {
            Group grp = new GroupHelper().GetGroupID(grpId);
            ViewData["Group"] = grp;
            List<Right> lstRight = new AdminHelper().getAllRight();
            ViewData["lstRight"] = lstRight;
            List<Template> lstTemplate = new AdminHelper().getAllTemplate();
            ViewData["lstTemplate"] = lstTemplate;
            return View("~/Views/Admin/RightManagement.aspx");
        }

        [HttpPost]
        public ActionResult AddGroupRight(FormCollection data)
        {
            int grpId = int.Parse(data.Get("grpID"));
            List<int> rightId = (List<int>)Session["StoreRight"];
            List<int> lst = (List<int>)Session["StoreTemplate"];
            new AdminHelper().addGroupRight(grpId, rightId, lst);
            return View("~/Views/Admin/AdminManagement.aspx");
        }

        [HttpPost]
        public void StoreRight(int value, int action)
        {

            if (action == 1)
            {//check
                if (Session["StoreRight"] == null)
                {
                    List<int> rightId = new List<int>();
                    rightId.Add(value);
                    Session["StoreRight"] = rightId;
                }
                else
                {
                    if (value == -1)// tất cả
                    {
                        List<int> rightId = (List<int>)Session["StoreRight"];
                        rightId.Clear();
                        rightId.Add(value);
                        Session["StoreTemplate"] = rightId;
                    }
                    else
                    {
                        List<int> rightId = (List<int>)Session["StoreRight"];
                        if (rightId.Contains(-1))
                        {
                            rightId.Clear();
                            rightId.Add(value);
                        }
                        else
                        {
                            rightId.Add(value);
                        }
                        Session["StoreRight"] = rightId;
                    }
                }
            }
            else//uncheck
            {
                if (value == -1)
                {
                    Session.Remove("StoreRight");
                }
                else
                {
                    List<int> rightId = (List<int>)Session["StoreRight"];
                    rightId.Remove(value);
                    Session["StoreRight"] = rightId;
                }
            }
        }

        [HttpPost]
        public void StoreTemplate(int value, int action)
        {
            if (action == 1)//check
            {
                if (Session["StoreTemplate"] == null)
                {
                    List<int> lst = new List<int>();
                    lst.Add(value);
                    Session["StoreTemplate"] = lst;
                }
                else
                {
                    if (value == -1)// tất cả
                    {
                        List<int> lst = (List<int>)Session["StoreTemplate"];
                        lst.Clear();
                        lst.Add(value);
                        Session["StoreTemplate"] = lst;
                    }
                    else
                    {
                        List<int> lst = (List<int>)Session["StoreTemplate"];
                        if (lst.Contains(-1))
                        {
                            lst.Clear();
                            lst.Add(value);
                        }
                        else
                        {
                            lst.Add(value);
                        }
                        Session["StoreTemplate"] = lst;
                    }
                }
            }
            else
            {
                if (value == -1)
                {
                    Session.Remove("StoreTemplate");
                }
                else
                {
                    List<int> lst = (List<int>)Session["StoreTemplate"];
                    lst.Remove(value);
                    Session["StoreTemplate"] = lst;
                }
            }
        }

    }
}
